#!/usr/bin/env python3

import codecs
import json
import http.client,urllib.parse,urllib.error
import time
import random
from get_proxy import *

class do_get_aminer_data(object):
    def __init__(self,name,university_name):
        self.name = name
        self.university_name = university_name
    def do_connect(self):
        params = urllib.parse.urlencode({
            'name': self.name,
            'aff': self.university_name
        })
        print(self.name + "--------------" + self.university_name)
        PROXY_POOL_URL = 'http://localhost:5555/random'
        get_proxy_data = get_proxy(PROXY_POOL_URL)
        proxy = get_proxy_data.get_proxy_data()

        proxies = {
            'http': "http://" + proxy
        }

        try:

            url = "https://api.aminer.cn/api/kit/gender/api/ch?%s"%params
            #print(url)
            response = requests.get(url,proxies=proxies)

            #print(response.text)
            if response.status_code == 200:
                print(response.status_code)
                data_json = json.loads(response.text)
                #time.sleep(random.randrange(5))
                return data_json,response.status_code
            else:
                #print("返回五百的值为：")
                #print(response.text)
                print(response.status_code)
                #time.sleep(10)
                return {},response.status_code

        except:
            print("222222")
            #time.sleep(10)
            return {},40303
